#include <stdio.h>
#include <errno.h>
#include <sys/shm.h>

#define MY_SHM_ID 2007 

int main(){
	int shmid;
	shmid=shmget(MY_SHM_ID,4096,0666|IPC_CREAT);
	if(shmid>=0){
		printf("create shared mem %d ok,pagesize:%d\n",shmid,getpagesize());
	}else{
		printf("create error\n");
	}

	shmid=shmget(MY_SHM_ID,0,0);
	struct shmid_ds shmds;
	int ret=shmctl(shmid,IPC_STAT,&shmds);
	if(ret==0){
		printf("size of memory segment is %d\n",shmds.shm_segsz);
		printf("number of attaches %d\n",(int)shmds.shm_nattch);
	}else{
		printf("shmctl failed (%d) \n",errno);
	}
	void *mem=shmat(shmid,(const void*)0,0);
	*((int*)mem)=123;
	shmdt(mem);

	mem=shmat(shmid,(const void*)0,0);
	printf("visit %d\n",*((int*)mem));
	shmdt(mem);
	return 0;
}
